package cn.dotalk.usermanage.learn;

import cn.dotalk.usermanage.entity.PersonEntity;

import java.util.*;
import java.util.stream.Stream;

/**
 * Person有个cityId Integer属性，给定一个List<Person>，按cityId进行分组，得到一个Map<Integer,List<Person>>
 */
public class Person {
    public static void main(String[] args) {
        List<PersonEntity> peoples = new ArrayList<>();
        peoples.add(new PersonEntity(1, "a"));
        peoples.add(new PersonEntity(1, "b"));
        peoples.add(new PersonEntity(1, "c"));
        peoples.add(new PersonEntity(2, "d"));
        peoples.add(new PersonEntity(3, "e"));
        peoples.add(new PersonEntity(3, "f"));

        System.out.println(peoples);
        Map<Integer, List<PersonEntity>> usersByCityId = new HashMap<>();
        for (PersonEntity p : peoples) {
            // todo
            //  1. map  判断key是否存在   存在 List 追加
            if (!usersByCityId.containsKey(p.cityId)) {
                //添加
                List<PersonEntity> tmpPList = new ArrayList<>();
                tmpPList.add(p);
                usersByCityId.put(p.cityId, tmpPList);
            } else {
                //追加
                List<PersonEntity> tmpPList = usersByCityId.get(p.cityId);
                tmpPList.add(p);
                usersByCityId.put(p.cityId, tmpPList);
            }

        }
        System.out.println(usersByCityId);


        System.out.println("====合并多个Map<Integer,List<Person>>，并且保证结果里的Person是不重复的====");
        List<PersonEntity> peoples2 = new ArrayList<>();
        peoples2.add(new PersonEntity(1, "aa"));
        peoples2.add(new PersonEntity(1, "aa"));
        peoples2.add(new PersonEntity(1, "b"));
        peoples2.add(new PersonEntity(1, "cc"));
        peoples2.add(new PersonEntity(2, "dd"));
        peoples2.add(new PersonEntity(3, "ee"));
        peoples2.add(new PersonEntity(3, "ff"));

        Map<Integer, List<PersonEntity>> usersByCityId2 = new HashMap<>();
        for (PersonEntity p : peoples2) {
            // todo
            //  1. map  判断key是否存在   存在 List 追加
            if (!usersByCityId2.containsKey(p.cityId)) {
                //添加
                List<PersonEntity> tmpPList = new ArrayList<>();
                tmpPList.add(p);
                usersByCityId2.put(p.cityId, tmpPList);
            } else {
                //追加
                List<PersonEntity> tmpPList = usersByCityId2.get(p.cityId);
                tmpPList.add(p);
                usersByCityId2.put(p.cityId, tmpPList);
            }

        }

        Map<Integer, List<PersonEntity>> combineResultMap = new HashMap<>();
        combineResultMap.putAll(usersByCityId);
        combineResultMap.putAll(usersByCityId2);

        // Stream combined = Stream.concat(usersByCityId.entrySet().stream(), usersByCityId2.entrySet().stream());
        // System.out.println("combined");
        // System.out.println(combined);

        System.out.println("map1");
        System.out.println(usersByCityId);
        System.out.println("map2");
        System.out.println(usersByCityId2);
        System.out.println("合并后");
        System.out.println(combineResultMap);
        // 通过有序结合来排重
        Map<Integer, List<PersonEntity>> newMap = new HashMap<>();
        combineResultMap.forEach((key, value) -> {
            HashSet tmpSet = new LinkedHashSet<>(value);
            System.out.println(tmpSet);
            // todo   tmpSet => List<?>
            // newMap.put(key,tmpSet.toArray());
        });

    }
}
